<?php

require_once '../MMysql.php';

/**
 * Created by PhpStorm.
 * User: hcadmin
 * Date: 16/8/18
 * Time: 上午9:17
 */


class propertyfuction
{
    public function addProperty($property_category,$property_agent_id,$property_city_id,$property_name,$property_dollar,
                                $property_rmb,$property_short_desc,$property_detail_desc,$property_imgs,$parameter_value1,
                                $parameter_value2,$parameter_value3,$parameter_value4,$parameter_value5,$latitude,$longitude)
//    public function addProperty()
    {

        $mysql = new MMysql();
       return $mysql->insert('property',array(
            'property_category'=>$property_category,
            'property_agent_id'=>$property_agent_id,
            'property_city_id'=>$property_city_id,
            'property_name'=>$property_name,
            'property_dollar'=>$property_dollar,
            'property_rmb'=>$property_rmb,
            'property_short_desc'=>$property_short_desc,
            'property_detail_desc'=>$property_detail_desc,
            'property_imgs'=>$property_imgs,
            'del_flag'=>0,
            'create_time'=>date("y-m-d",time()),
            'parameter_value1'=>$parameter_value1,
            'parameter_value2'=>$parameter_value2,
            'parameter_value3'=>$parameter_value3,
            'parameter_value4'=>$parameter_value4,
            'parameter_value5'=>$parameter_value5,
            'latitude'=>$latitude,
            'longitude'=>$longitude,
       ));
    }

    public function deleteProperty($property_id){

        $mysql = new MMysql();

        return $mysql->where(array('property_id'=>$property_id))->update('property',array(
            'del_flag'=>1,
        ));
    }

    public function updateProperty($property_category,$property_agent_id,$property_city_id,$property_name,$property_dollar,
                                $property_rmb,$property_short_desc,$property_detail_desc,$property_imgs,$parameter_value1,
                                $parameter_value2,$parameter_value3,$parameter_value4,$parameter_value5,$property_id,
                                   $latitude,$longitude)
    {
        $mysql = new MMysql();

        return $mysql->where(array('property_id'=>$property_id))->update('property',array(
            'property_category'=>$property_category,
            'property_agent_id'=>$property_agent_id,
            'property_city_id'=>$property_city_id,
            'property_name'=>$property_name,
            'property_dollar'=>$property_dollar,
            'property_rmb'=>$property_rmb,
            'property_short_desc'=>$property_short_desc,
            'property_detail_desc'=>$property_detail_desc,
            'property_imgs'=>$property_imgs,
            'parameter_value1'=>$parameter_value1,
            'parameter_value2'=>$parameter_value2,
            'parameter_value3'=>$parameter_value3,
            'parameter_value4'=>$parameter_value4,
            'parameter_value5'=>$parameter_value5,
            'latitude'=>$latitude,
            'longitude'=>$longitude,
        ));
    }

    public function selectProperty($type,$page,$rows)
    {

        $mysql = new MMysql();
        $res = $mysql
            ->where(array('property_category'=>$type,"del_flag"=>0))
            ->limit($page,$rows)
            ->order(array('property_id'=>'desc'))
            ->select('property');
        $arraya=Array();
        foreach ($res as $u){
            $u["likes"]=$mysql->where(array('property_id'=> $u["property_id"]))->count('likes');

            //城市
            $city_en_name=$mysql->where(array('city_id'=> $u["property_city_id"]))->field(array('city_en_name'))->select('city');
            $u["city"]=$city_en_name[0]['city_en_name'];

            //代理商
            $agentname=$mysql->where(array('agent_id'=> $u["property_agent_id"]))->select('agent');
            $u["agent"]=$agentname[0]['agent_name'];

            $arraya[]=$u;
        }

        $count=$mysql->where(array('property_category'=>$type,"del_flag"=>0))->count('property');

        $array=Array();
        $array["rows"]=$arraya;
        $array["total"]=$count;
        echo json_encode($array);
    }

    public function selectbyagent($type,$page,$rows,$agent_id)
    {

        $mysql = new MMysql();
        $res = $mysql
            ->where(array('property_category'=>$type,'property_agent_id'=>$agent_id,"del_flag"=>0))
            ->limit($page,$rows)
            ->select('property');
        $arraya=Array();
        foreach ($res as $u){
            $u["likes"]=$mysql->where(array('property_id'=> $u["property_id"]))->count('likes');

            //城市
            $city_en_name=$mysql->where(array('city_id'=> $u["property_city_id"]))->field(array('city_en_name'))->select('city');
            $u["city"]=$city_en_name[0]['city_en_name'];

            //代理商
            $agentname=$mysql->where(array('agent_id'=> $u["property_agent_id"]))->select('agent');
            $u["agent"]=$agentname[0]['agent_name'];

            $arraya[]=$u;
        }

        $count=$mysql->where(array('property_category'=>$type,'property_agent_id'=>$agent_id,"del_flag"=>0))->count('property');

        $array=Array();
        $array["rows"]=$arraya;
        $array["total"]=$count;
        echo json_encode($array);
    }
}


$method =$_REQUEST["method"];

if($method=='list'){

    $d=new propertyfuction();

    return $d->selectProperty($_REQUEST["type"],$_REQUEST["page"],$_REQUEST["rows"]);

}
else if($method=='list_agent'){

    $d=new propertyfuction();

    return $d->selectbyagent($_REQUEST["type"],$_REQUEST["page"],$_REQUEST["rows"],$_REQUEST["agent_id"]);

}
else if($method=='add'){
    $d=new propertyfuction();

//    echo $_REQUEST["parameter_value"];

    echo $d->addProperty(
        $_REQUEST["property_category"],$_REQUEST["property_agent_id"],$_REQUEST["property_city_id"],
        $_REQUEST["property_name"],$_REQUEST["property_dollar"],$_REQUEST["property_rmb"],
        $_REQUEST["property_short_desc"],$_REQUEST["property_detail_desc"],$_REQUEST["property_imgs"],
        $_REQUEST["parameter_value1"],$_REQUEST["parameter_value2"],$_REQUEST["parameter_value3"],
        $_REQUEST["parameter_value4"],$_REQUEST["parameter_value5"],$_REQUEST["latitude"],$_REQUEST["longitude"]);
}

else if($method=='delete'){
    $d=new propertyfuction();
    echo $d->deleteProperty($_REQUEST["property_id"]);
}
else if($method=='update'){
    $d=new propertyfuction();
    echo $d->updateProperty(
        $_REQUEST["property_category"],$_REQUEST["property_agent_id"],$_REQUEST["property_city_id"],
        $_REQUEST["property_name"],$_REQUEST["property_dollar"],$_REQUEST["property_rmb"],
        $_REQUEST["property_short_desc"],$_REQUEST["property_detail_desc"],$_REQUEST["property_imgs"],
        $_REQUEST["parameter_value1"],$_REQUEST["parameter_value2"],$_REQUEST["parameter_value3"],
        $_REQUEST["parameter_value4"],$_REQUEST["parameter_value5"],$_REQUEST["property_id"],$_REQUEST['latitude'],$_REQUEST['longitude']);
}
